app.controller("z-role-c", [ '$scope','$rootScope','$z-http','$sce','z-role-s','z-corporation-s','z-attendance-s','z-department-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,CorporationService,AttendanceService,DepartmentService,MenuService,$filter) {
	$scope.viewModel = {};
	$scope.viewModel.perPage = "10";
	$scope.viewModel.rowCount = "1";
	$scope.viewModel.role = "";
	$scope.editModel = {};
	$scope.editModel.role={};
	$scope.editModel.title="";
	$scope.viewModel.employeeNo=null;
	$scope.viewModel.employeeList=[];
	$scope.viewModel.corporationList=[];
	
	$scope.importModel = {};
	$scope.importModel.roleCode ="";
	$scope.importModel.roleName="";
	$scope.importModel.parentCode="";
	$scope.importModel.parentName="";
	$scope.importModel.roleLevel="";
	
	//下拉多选框长度
	$scope.multiSelectMaxHeight=300;
	
	// 定义生效时间框内的值，用于检索
	$scope.editModel.changeEffectiveDate = new Date();
	// 获取当天日期
	$scope.viewModel.effectiveDate = new Date();
	//查询页面名称
	MenuService.getMenuByUrl({url:'role'},
	function(data){	
		$scope.viewModel.menuList=data;
	},function(error) {});
	
	//加载考勤状态数据
	$scope.viewModel.bindSelectData = function(){
		AttendanceService.getDictionaryListBydicCls({
			dicCls:"9"
		},
		function(data){
			$scope.editModel.requestRoleData= data;
		},function(error) {
					
		});
		
		//查询公司
		CorporationService.getCorporationList({
			fromDate:$scope.viewModel.changeEffectiveDate,
		},
		function(data){
			var site = [];  
	        $.each(data, function(index, values) {  
	            site.push({  
	                label : "("+values.corporationCode+")"+values.corporationName,  
	                value : values.corporationCode  
	            });  
	        });  
	        $('#rCorporation').multiselect('dataprovider',site);
		},function(error) {
			
		});
	};
	$scope.viewModel.bindSelectData();
	
	//公司
	$('#rCorporation').multiselect({
		includeSelectAllOption: true,
		enableFiltering: true,
		delimiterText: '; ',
        maxHeight: $scope.multiSelectMaxHeight
    });
	
	//加载
	$scope.viewModel.load=function(){
		$scope.viewModel.show=true;
	};
	//关闭加载
	$scope.viewModel.unLoad=function(){
		$scope.viewModel.show=false;
	};
	
	//新增
	$scope.viewModel.add = function(){
		$scope.viewModel.show = true;
		$scope.editModel.title="新增岗位";
		$scope.editModel.type = true;
		$scope.editModel.role={};
		//验证不能为空
		$scope.editForm.roleName.$dirty=false;
		$scope.editForm.roleCode.$dirty=false;
	};
	
	//修正
	$scope.viewModel.update=function(role){
		$scope.viewModel.load();
		$scope.editModel.title="修改岗位";
		$scope.editModel.type = false;
		$scope.editModel.role=angular.copy(role);
		$scope.editModel.role.roleId = role.roleId;
	};

	//验证
	$scope.viewModel.requiredForm= function() {
        if (!$scope.editForm.$valid) {
        	if(!$scope.editForm.roleName.$dirty){
        		$scope.editForm.roleName.$dirty=true;
        	}
        	if(!$scope.editForm.roleCode.$dirty){
        		$scope.editForm.roleCode.$dirty=true;
        	}
            return true;
        }else{
        	if($scope.editModel.role.departmentId==null){
        		$rootScope.alertWarning("未选择部门！");
        		return true;
        	}
        	return false;
        }
    };
	
	//保存
	$scope.viewModel.save = function(){
		if($scope.viewModel.requiredForm()){
			return;
		}
		$scope.viewModel.isLoading = true;
		$scope.editModel.role.fromDate = $scope.editModel.changeEffectiveDate;
		if($scope.editModel.type){
			Service.saveRole($scope.editModel.role,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		}else{
			Service.updateRole($scope.editModel.role,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		}
	};
	
	//导出
	$scope.viewModel.educe = function(){
		$scope.viewModel.isLoading = true;
	     Service.getRoleExport(
	    	function(data){
				$scope.viewModel.isLoading = false;
				window.location.href = data;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading = false;
			});
	};
	
	//删除
	$scope.viewModel.del=function(role){
		$rootScope.alertConfirm("确定删除吗？",function() {
			$scope.viewModel.isLoading=true;
			Service.deleteRole({
				roleId:role.roleId
			},function(){
				$scope.viewModel.bindData();
				$scope.viewModel.isLoading=false;
				$rootScope.alertInfo("删除成功。");
			},function(error){
				$scope.viewModel.isLoading=false;
				$rootScope.alertError(error.fault.message);
			});
		});
	};

	/*   上级岗位选择   */
	$scope.viewSelectUpRoleModel={};
	$scope.viewSelectUpRoleModel.role = {};
	$scope.viewSelectUpRoleModel.perPage = "10";
	$scope.viewSelectUpRoleModel.rowCount = "1";
	// 所属部门选择
	$scope.viewSelectUpDepartmentModel={};
	$scope.viewSelectUpDepartmentModel.perPage = "10";
	$scope.viewSelectUpDepartmentModel.rowCount = "1";
	$scope.viewSelectUpDepartmentModel.role = {};
	$scope.viewSelectUpDepartmentModel.role.corporation="";
	
	//加载岗位数据
//	$scope.viewSelectUpRoleModel.bindData = function(departmentId){
//		$scope.viewSelectUpRoleModel.isLoading=true;
//		//获取基本信息
//		Service.getRoleList({
//			pageNo:$scope.viewSelectUpRoleModel.rowCount,
//			pageCount:$scope.viewSelectUpRoleModel.perPage,
//			departmentId:departmentId,
//			fromDate:$scope.editModel.changeEffectiveDate,
//		},
//		function(data){
//			$scope.viewSelectUpRoleModel.dataList = data.roleBeanList;
//			$scope.viewSelectUpRoleModel.totalData = data.pageTotal;
//			$scope.viewSelectUpRoleModel.isLoading=false;
//		},function(error) {
//			$rootScope.alertError(error.fault.message);
//			$scope.viewSelectUpRoleModel.isLoading=false;
//		});
//	};
	//加载公司数据
	$scope.viewSelectUpRoleModel.CorbindData = function(){
		$scope.viewSelectUpRoleModel.isLoading=true;
		//获取基本信息
		CorporationService.getCorporationList({
			fromDate:$scope.editModel.changeEffectiveDate,
		},
		function(data){
			$scope.viewSelectUpRoleModel.dataCorList = data;
			$scope.viewSelectUpRoleModel.isLoading=false;
		},function(error) {
			$rootScope.alertError(error.fault.message);
			$scope.viewSelectUpRoleModel.isLoading=false;
		});
	};
	// 上级岗位选择按钮
	$scope.editModel.chooseUpde = function(){
		if($scope.editModel.role.departmentId == null){
			$rootScope.alertWarning("请先选所属部门，再选上级岗位。");
			return;
		}
		$scope.viewSelectUpRoleModel.dataList = "";
		if($scope.editModel.type==false){
			$scope.viewSelectUpRoleModel.departRole($scope.editModel.role.departmentId);
		}
		$scope.viewSelectUpRoleModel.show=true;
		$scope.viewSelectUpRoleModel.title="上级岗位选择";
		$scope.viewSelectUpRoleModel.role.department = "";
	};
	
	//判断是否选中
	$scope.viewSelectUpRoleModel.isInAssignmentArr=function(role){
		if(role.roleId==$scope.editModel.role.parentId){
			return true;
		}
		return false;
	};
	
	//判断是否选中
	$scope.viewSelectUpDepartmentModel.isInAssignmentArr=function(department){
		if(department.departmentId==$scope.editModel.role.departmentId){
			return true;
		}
		return false;
	};

	// 所属部门选择按钮
	$scope.editModel.chooseDepartment = function(){
		$scope.viewSelectUpDepartmentModel.isLoading=true;
		$scope.viewSelectUpDepartmentModel.dataDepartmentList = "";
		// 调用公司信息
		$scope.viewSelectUpRoleModel.CorbindData();
		$scope.viewSelectUpDepartmentModel.show=true;
		$scope.viewSelectUpDepartmentModel.title="所属部门选择";
		$scope.viewSelectUpDepartmentModel.role.corporation = "";
	};
	
	// 通过选择公司筛选部门
	$scope.viewSelectUpDepartmentModel.corDepart = function(corId){
		if(corId != undefined){
			//获取基本信息
			DepartmentService.getDepartmentList({
				pageNo:$scope.viewSelectUpDepartmentModel.rowCount,
				pageCount:$scope.viewSelectUpDepartmentModel.perPage,
				departmentName:$scope.viewSelectUpDepartmentModel.departmentName,
				corporationId:corId,
				fromDate:$scope.editModel.changeEffectiveDate,
			},
			function(data){
				$scope.viewSelectUpDepartmentModel.dataDepartmentList = data.departmentBeanList;
				$scope.viewSelectUpDepartmentModel.totalData = data.pageTotal;
				$scope.viewSelectUpDepartmentModel.isLoading=false;
			},function(error) {
				$scope.viewSelectUpDepartmentModel.isLoading=false;
			});
		}else{
			$scope.viewSelectUpDepartmentModel.dataDepartmentList = "";
		}
	}
	
	//关闭加载
	$scope.viewSelectUpRoleModel.unLoad=function(){
		$scope.viewSelectUpRoleModel.show=false;
		$scope.viewSelectUpDepartmentModel.show=false;
	};
	
	// 点击选择指定上级岗位按钮
	$scope.viewSelectUpRoleModel.upRole = function(role){
		$scope.viewSelectUpRoleModel.show=false;
		$scope.viewModel.show=true;
		$scope.editModel.role.parentName = role.roleName;
		$scope.editModel.role.parentCode= role.roleCode;
		$scope.editModel.role.parentId = role.roleId;
	};
	
	//清除
	$scope.editModel.delRole=function(){
		$scope.editModel.role.parentName = null;
		$scope.editModel.role.parentCode= null;
		$scope.editModel.role.parentId = null;
	};
		
	//清除
	$scope.editModel.delDepartment=function(){
		$scope.editModel.role.departmentName = null;
		$scope.editModel.role.departmentCode = null;
		$scope.editModel.role.departmentId = null;
	};
	
	// 点击选择指定所属部门按钮
	$scope.viewSelectUpDepartmentModel.upDepartment = function(department){
		$scope.viewSelectUpDepartmentModel.show=false;
		$scope.viewModel.show=true;
		$scope.editModel.role.departmentName = department.departmentName;
		$scope.editModel.role.departmentCode = department.departmentCode;
		$scope.editModel.role.departmentId = department.departmentId;
	};
	// 通过选择部门筛选岗位
	$scope.viewSelectUpRoleModel.departRole = function(departmentId){
		if(departmentId != undefined){
			$scope.viewSelectUpRoleModel.isLoading=true;
			//获取基本信息
			Service.getRoleList({
				pageNo:$scope.viewSelectUpRoleModel.rowCount,
				pageCount:$scope.viewSelectUpRoleModel.perPage,
				departmentId:departmentId,
				fromDate:$scope.editModel.changeEffectiveDate,
			},
			function(data){
				$scope.viewSelectUpRoleModel.dataList = data.roleBeanList;
				$scope.viewSelectUpRoleModel.totalData = data.roleBeanList.length;
				$scope.viewSelectUpRoleModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewSelectUpRoleModel.isLoading=false;
			});
		}else{
			$scope.viewSelectUpRoleModel.dataList = "";
		}
	}
	$scope.viewModel.effectiveDateClose=function(){
    	if($scope.viewModel.radioValue==3){
    		
    	}else{
    		$scope.viewModel.effectiveDate=null;
    	}
    }

	//日期控件
	$scope.viewModel.dateOptions = {
			formatYear: 'yy',
		    startingDay: 1,
		    showWeeks:false
	};
	//日期控件
	$scope.viewModel.format = "yyyy/MM/dd";
	//日期启动开关
	$scope.viewModel.effectiveDateOpen = false;
	$scope.viewModel.effectiveDateOpnened = false;
	$scope.viewModel.effectiveDateOpen = function($event) {
		$event.preventDefault();
		$event.stopPropagation();
		$scope.viewModel.effectiveDateOpnened = !$scope.viewModel.effectiveDateOpnened;

		
	};
	//加载数据
	$scope.viewModel.bindData = function(changeEffectiveDate){
		$scope.viewModel.employeeList=[];
		if($scope.viewModel.employeeNo!=null){
			var employeeNoData = $scope.viewModel.employeeNo.split("\n");
			for(var i=0;i<employeeNoData.length;i++){
				$scope.viewModel.employeeList.push(employeeNoData[i]);
			}
		}
		if(changeEffectiveDate == 1){
			$scope.editModel.changeEffectiveDate = $scope.viewModel.effectiveDate;
		}
		$scope.viewSelectUpRoleModel.CorbindData();
		$scope.viewSelectUpDepartmentModel.corDepart();
		$scope.viewModel.isLoading=true;
		//获取基本信息
		Service.getRoleList({
			pageNo:$scope.viewModel.rowCount,
			pageCount:$scope.viewModel.perPage,
			employeeList:$scope.viewModel.employeeList,
			roleName:$scope.viewModel.role,
			fromDate:$scope.viewModel.effectiveDate,
			corporationList:$scope.viewModel.corporationList
		},
		function(data){
			$scope.viewModel.dataList = data.roleBeanList;
			$scope.viewModel.totalData = data.pageTotal;
			$scope.viewModel.isLoading=false;
		},function(error) {
			$scope.viewModel.isLoading=false;
		});
	};
	$scope.viewModel.bindData();
} ]);